<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教务管理系统 - 登录</title>
    <style>
        body { background: #f0f2f5; font-family: Arial, sans-serif; }
        .login-container {
            width: 350px;
            margin: 100px auto 0 auto;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 12px #e0e0e0;
            padding: 36px 32px 28px 32px;
        }
        .login-title {
            text-align: center;
            font-size: 1.7em;
            color: #007BFF;
            margin-bottom: 28px;
            font-weight: bold;
        }
        .login-form label {
            display: block;
            margin-bottom: 6px;
            color: #555;
        }
        .login-form input {
            width: 100%;
            padding: 8px 12px;
            margin-bottom: 18px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 1em;
        }
        .login-form button {
            width: 100%;
            background: #007BFF;
            color: #fff;
            border: none;
            padding: 10px 0;
            border-radius: 4px;
            font-size: 1.1em;
            cursor: pointer;
            margin-top: 8px;
        }
        .login-form button:hover { background: #0056b3; }
        .login-tip {
            text-align: center;
            color: #888;
            font-size: 0.95em;
            margin-top: 18px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-title">教务管理系统登录</div>
        <form class="login-form" onsubmit="login(event)">
            <label for="username">账号</label>
            <input type="text" id="username" name="username" placeholder="请输入账号" required>
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <button type="submit">登录</button>
        </form>
        <div class="login-tip">学生账号/密码为学号，老师请联系管理员获取账号</div>
    </div>
    <script>
        // 检查是否已登录，如果已登录则直接跳转到首页
        document.addEventListener('DOMContentLoaded', function() {
            var user = localStorage.getItem('loginUser');
            var role = localStorage.getItem('userRole');
            if (user && role) {
                window.location.href = 'index.html';
            }
        });

        function login(e) {
            e.preventDefault();
            var username = document.getElementById('username').value;
            var password = document.getElementById('password').value;

            if (!username || !password) {
                alert('请输入用户名和密码');
                return;
            }

            // 调用后端登录接口
            fetch('/api/user/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
            .then(response => {
                if (!response.ok) {
                    if (response.status === 401) {
                        throw new Error('用户名或密码错误');
                    } else {
                        throw new Error('登录请求失败，状态码: ' + response.status);
                    }
                }
                return response.json();
            })
            .then(data => {
                if (data.success) {
                    // 登录成功，保存用户信息和角色
                    localStorage.setItem('loginUser', username);
                    localStorage.setItem('userRole', data.role);
                    localStorage.setItem('userName', data.name || username);
                    localStorage.setItem('userId', data.id);

                    // 跳转到首页
                    window.location.href = 'index.html';
                } else {
                    // 登录失败
                    alert(data.message || '登录失败，请检查账号和密码');
                }
            })
            .catch(error => {
                console.error('登录请求失败:', error);

                // 特殊处理：如果后端API尚未实现，使用临时的前端验证（仅用于开发阶段）
                if (username === 'admin' && password === 'admin123') {
                    localStorage.setItem('loginUser', username);
                    localStorage.setItem('userRole', 'admin');
                    localStorage.setItem('userName', '管理员');
                    localStorage.setItem('userId', '1');
                    window.location.href = 'index.html';
                } else {
                    alert(error.message || '账号或密码错误，或请联系管理员获取账号');
                }
            });
        }
    </script>
</body>
</html>
